/**
 * 
 */

$(function() {
	$("#languageId").change(function() {
		var language = $(this).children("option:selected").attr("data-key");
		if (language == 'zh') {
			$(".zh-category").addClass("defaultForm");
			$(".en-category").removeClass("defaultForm");
		} else {
			$(".zh-category").removeClass("defaultForm");
			$(".en-category").addClass("defaultForm");
		}
	})

	$(".submit").click(function() {
						var categorys = [];
						var index=0;
						$("fieldset").each(function(j) {
							var zTree = $.fn.zTree.getZTreeObj($(this).find(".ztree").attr("id"));
							$(this).find("li[id]").each(function(){
								var node = zTree.getNodeByParam("tId", $(this).attr("id"));										
								var category = {};
								category["categoryName"] = node.categoryName;
								category["productCategoryId"] = node.productCategoryId;
								category["parentCategoryId"] = node.parentCategoryId;
								category["categoryType"] = node.categoryType;
								category["languageId"] = node.languageId;
								categorys[index++] = category;												
							})
						})
						$.ajax({
							url : ctx + "/category/save2.do",
							data : {
								category : $.toJSON(categorys)
							},
							type : "POST",
							success : function(data) {
								showInfo(data, function() {
									getCategory();
								})
							}
						})
					})
	getCategory();
	function getCategory() {
		var languageMap = {};
		$("#languageId option").each(
				function() {
					languageMap["languageId_" + $(this).val()] = $(this).attr(
							"data-key");
				})
		var typeMap = {
			type_0 : "left",
			type_1 : "right"
		};
		$.getJSON(ctx + "/category/getAll.do", function(data) {
			if (data) {
						
				$("ul.subCategory").empty();
				$.each(data, function(i, category) {
					var key = eval("languageMap.languageId_"
							+ category.languageId);
					var categoryDiv = $("." + key + "-category");
					var type = eval("typeMap.type_" + category.categoryType);
					var categoryFieldset = categoryDiv.find(".column-" + type);
					var ztree = new zTreeUtil({data:category, object: categoryFieldset.find("ul.ztree")});
				})
				 
			}
		})
	}
})
function zTreeUtil(opts){
	var data = opts.data;
	var treeObj = opts.object;
	var setting = {
		check : {
			//enable : true
		},
		view : {
			addHoverDom : addHoverDom,
			removeHoverDom : removeHoverDom,
			selectedMulti : false,
			dblClickExpand: dblClickExpand
		},
		edit : {
			enable : true,
			editNameSelectAll : true,
			showRemoveBtn : showRemoveBtn,
			showRenameBtn : showRenameBtn
		},
		data : {
			key : {
				checked : "checked",
				children : "subCategorys",
				name : "categoryName"
			},
			simpleData : {
				enable : true,
				idKey: "productCategoryId",
				pIdKey: "parentCategoryId"
			}
		},
		callback : {
			beforeDrag : beforeDrag,
			beforeEditName : beforeEditName,
			beforeRemove : beforeRemove,
			beforeRename : beforeRename,
			onRemove : onRemove,
			onRename : onRename
		}
	};

	setting.check.chkboxType = {
		"Y" : "s",
		"N" : "s"
	};
	var zTree = $.fn.zTree.init(treeObj, setting, data);
	zTree.expandAll(true);
	var className = "dark";
	function beforeDrag(treeId, treeNodes) {
		return false;
	}
	function beforeEditName(treeId, treeNode) {
		className = (className === "dark" ? "" : "dark");
		zTree.selectNode(treeNode);
		return true;
	}
	function beforeRemove(treeId, treeNode) {
		className = (className === "dark" ? "" : "dark");
		zTree.selectNode(treeNode);
		return confirm("確定刪除節點 -- " + treeNode.categoryName + "？");
	}
	function onRemove(e, treeId, treeNode) {
		if(treeNode.productCategoryId < 0)
			return;
		$.ajax({
			url : ctx+"/category/delete.do",
			data : {categoryId : treeNode.productCategoryId},
			type : "POST",
			success: function(data){
				showInfo(data, function(){					
				})
			},
			error: function(r){
				showInfo(r, function(){});
			}
		})
	}
	function beforeRename(treeId, treeNode, newName) {
		className = (className === "dark" ? "" : "dark");
		if (newName.length == 0) {
			alert("节点名称不能为空.");
			setTimeout(function() {
				zTree.editName(treeNode)
			}, 10);
			return false;
		}
		return true;
	}
	function onRename(e, treeId, treeNode) {
	
	}
	function showRemoveBtn(treeId, treeNode) {
		return treeNode.level > 0;
	}
	function showRenameBtn(treeId, treeNode) {
		return treeNode.level > 0;
	}
	var newCount = 1;
	function addHoverDom(treeId, treeNode) {
		if(treeNode.level > 1)
			return;
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_" + treeNode.productCategoryId).length > 0)
			return;
		var addStr = "<span class='button add' id='addBtn_" + treeNode.productCategoryId
				+ "' title='add node' onfocus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_" + treeNode.productCategoryId);
		if (btn)
			btn.bind("click", function() {
				zTree.addNodes(treeNode, {
					productCategoryId : -(new Date()).getMilliseconds(),
					parentCategoryId : treeNode.productCategoryId,
					categoryType: treeNode.categoryType,
					languageId : treeNode.languageId,
					categoryName : "new node" + (newCount++)
				});
				return false;
			});
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_" + treeNode.productCategoryId).unbind().remove();
	};
	function dblClickExpand(treeId, treeNode) {
		return treeNode.level > 0;
	} 
}